package com.wing.warehouse.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.platform.feign.PlatformClient;
import com.wing.platform.order.model.response.InventoryJobBoxLabelVO;
import com.wing.platform.order.model.response.InventoryProductVO;
import com.wing.warehouse.dao.BoxLabelDao;
import com.wing.warehouse.model.entity.BoxLabel;
import com.wing.warehouse.model.request.BoxLabelForm;
import com.wing.warehouse.model.response.BoxLabelVO;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 料箱存储包装编码表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class BoxLabelServiceImpl extends ServiceImpl<BoxLabelDao, BoxLabel> implements BoxLabelService {

    @Autowired
    private PlatformClient platformClient;

    @Override
    public int add(BoxLabelForm boxLabelForm) {

        //校验

        BoxLabel boxLabel =new BoxLabel();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(BoxLabel.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(boxLabelForm, boxLabel, copyOptions);

        return baseMapper.insert(boxLabel);
    }

    @Override
    public List<BoxLabelVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<BoxLabelVO> pageList4app(Page page, Map<String, Object> params) {
        List<BoxLabelVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public BoxLabelVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<Object> getBoxLabBySkuCodeAndWarehouseIdList(String skuCode,Long warehouseId,String sourceType) {
        List<Object> listObject = Lists.newArrayList();
        if(sourceType.equals("0")){
            List<BoxLabelVO> list = baseMapper.getBoxLabBySkuCodeAndWarehouseIdList(skuCode, warehouseId);
            listObject.addAll(list);
            return listObject;
        }else if(sourceType.equals("1")){
            JsonResult<List<InventoryJobBoxLabelVO>> jsonResult = platformClient.getBoxLabBySkuCodeAndWarehouseId(skuCode, warehouseId);
            List<InventoryJobBoxLabelVO> inventoryJobBoxLabelList = jsonResult.getData();
            for (InventoryJobBoxLabelVO item:inventoryJobBoxLabelList){
                listObject.add(item);
            }
            return listObject;
        }
        return null;
    }

    @Override
    public BoxLabelVO getBoxLabBySkuAndWarehouseId(String skuCode, Long warehouseId) {
        return baseMapper.getBoxLabBySkuAndWarehouseId(skuCode,warehouseId);
    }
}
